/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lvyh.lightframe.sample.controller.solr;

import com.google.common.base.Joiner;
import com.lvyh.lightframe.solr.common.SolrPageParam;
import com.lvyh.lightframe.solr.common.SolrQueryCondition;
import com.lvyh.lightframe.solr.entity.Vlog;
import com.lvyh.lightframe.solr.manager.SolrClientManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.solr.client.solrj.SolrQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author lvyh 2021/03/09.
 */
@RestController
@Slf4j
public class SolrController {

    @Autowired
    private SolrClientManager solrClientManager;

    //http://localhost:8080/solr-query
    @RequestMapping("/solr-query")
    public String solrQuery() throws Exception {

        SolrQueryCondition<Vlog> condition = new SolrQueryCondition<>();
        List<String> visitIdList = Arrays.asList("3e15d77bb3eaba6ed62d2b0bbbca5ebc", "588701e22a71e4d1f1c90eeff3bf0d4a");
        String inParam = "(" + Joiner.on(" OR ").join(visitIdList) + ")";

        Vlog vlog = new Vlog();
        vlog.setVid(inParam);
        vlog.setUid("2");
        vlog.setUnique("33");
        condition.setData(vlog);

        Map<String, SolrQuery.ORDER> sort = new HashMap<>();
        sort.put("update_time", SolrQuery.ORDER.desc);
        SolrPageParam pageParam = new SolrPageParam();
        pageParam.setPageNo(0);
        pageParam.setPageSize(5);
        List<Vlog> searchList = solrClientManager.search(condition, Vlog.class, sort, pageParam);
        log.info("searchList:{}", searchList);
        return "ok";
    }

    //http://localhost:8080/solr-add
    @RequestMapping("/solr-add")
    public String solrAdd() throws Exception {

        Vlog vlogBean = new Vlog();
        vlogBean.setVid("3e15d77bb3eaba6ed62d2b0bbbca5ebc");
        vlogBean.setRowId(UUID.randomUUID().toString());
        solrClientManager.createIndex(vlogBean);
        return "ok";
    }

}